const Cesium=window.Cesium
import CreateRegularPolygon from "../../Draw/Polygon/CreatePolygon";
import { newSessionid} from "../../plotCommon"
export default class Water{
    _viewer:any
    _changeDepth:Boolean
    _WaterAnalyseID:Array<any>
    constructor(viewer){
       this._changeDepth=false
       this._WaterAnalyseID=[]
        this._viewer=viewer  
    }
    get waterAnalyseID(){
        return this._WaterAnalyseID
    }
    create(callbackForResult,option){
        let imageUrl=option.imgUrl||""
        console.log(imageUrl,'imageUrl');
        
        let viewer=this._viewer
        if (!viewer.terrainProvider._layers) {
            if (callbackForResult && typeof callbackForResult == "function")
              callbackForResult("请先开启地形");
            return;
          }
      
          if (viewer.scene.globe.depthTestAgainstTerrain == false)
            viewer.scene.globe.depthTestAgainstTerrain = true;
          this._changeDepth = true;
          const callback = (po) => {
            viewer.entities.remove(po);
            let prid  =newSessionid(32);
            //底水
            let waterPrimitive = new Cesium.Primitive({
              id: prid,
              allowPicking: false,
              asynchronous: false,
              geometryInstances: new Cesium.GeometryInstance({
                id: "initRiver",
                geometry: new Cesium.PolygonGeometry({
                  polygonHierarchy: new Cesium.PolygonHierarchy(
                    po.PottingPoint.positions
                  ),
                  vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
                  extrudedHeight: 100,
                  height: 0,
                }),
              }),
              appearance: new Cesium.EllipsoidSurfaceAppearance({
                aboveGroud: true,
                material: new Cesium.Material({
                  fabric: {
                    type: "Water",
                    uniforms: {
                      blendColor: new Cesium.Color(0, 0, 1, 0.3),
                      // normalMap: require("./extend/analyse/water/water.jpg"),
                      normalMap:imageUrl,
                      //频率速度设置
                      frequency: 200,
                      animationSpeed: 0.01,
                      amplitude: 10,
                    },
                  },
                }),
              }),
            });
            this._WaterAnalyseID.push(waterPrimitive);
            
            viewer.scene.primitives.add(waterPrimitive);
            let enid = newSessionid(32);
            this._WaterAnalyseID.push(enid);
            var waterHeight = 100;
            //升起的材质
            viewer.entities.add({
              id: enid,
              polygon: {
                hierarchy: po.PottingPoint.positions,
                material: Cesium.Color.LIGHTBLUE.withAlpha(0.5),
              
                extrudedHeight: new Cesium.CallbackProperty(function () {
                  return waterHeight;
                }, false),
              },
              material: new Cesium.Material({
                fabric: {
                  type: "Water",
                  uniforms: {
                    blendColor: new Cesium.Color(0, 0, 1, 0.3),
                    // normalMap: require("./extend/analyse/water/water.jpg"),
                    normalMap:imageUrl,
                    //频率速度设置
                    frequency: 200,
                    animationSpeed: 0.01,
                    amplitude: 10,
                  },
                },
              }),
            });

            //升起的材质变化成水
            // let waterRaisePrimitive = new Cesium.Primitive({
            //   id: enid,
            //   allowPicking: false,
            //   asynchronous: false,
            //   geometryInstances: new Cesium.GeometryInstance({
            //     id: "raiseRiver",
            //     geometry: new Cesium.PolygonGeometry({
            //       polygonHierarchy: new Cesium.PolygonHierarchy(
            //         po.PottingPoint.positions
            //       ),
            //       vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
            //       extrudedHeight: new Cesium.CallbackProperty(function () {
            //         return waterHeight;
            //       }, false), // 使用 CallbackProperty
            //       // height: 0,
            //     }),
            //   }),
            //   appearance: new Cesium.EllipsoidSurfaceAppearance({
            //     aboveGroud: true,
            //     material: new Cesium.Material({
            //       fabric: {
            //         type: "Water",
            //         uniforms: {
            //           blendColor: new Cesium.Color(0, 0, 1, 0.3),
            //           // normalMap: require("./extend/analyse/water/water.jpg"),
            //           normalMap: imageUrl,
            //           // 频率速度设置
            //           frequency: 200,
            //           animationSpeed: 0.01,
            //           amplitude: 10,
            //         },
            //       },
            //     }),
            //   }),
            // });
            
            // // 将 Primitive 添加到场景中
            // viewer.scene.primitives.add(waterRaisePrimitive);

            //height随时间变化
            viewer.clock.onTick.addEventListener(function () {
              if (waterHeight > 2500) {
                waterHeight = 2200;
              }
              waterHeight += 1;
            });
          };
          var canvas = viewer.scene.canvas;
          let handler = new Cesium.ScreenSpaceEventHandler(canvas);
          CreateRegularPolygon(
            viewer,
            handler,
            [],
            {
              onground: false,
            },
            callback
          );
    }
    removeAll(){
        let viewer=this._viewer
        if (this._WaterAnalyseID.length > 0)
        this._WaterAnalyseID.forEach((item, index) => {
          if (index % 2 === 0) {
            // 偶数索引处理逻辑
            viewer.scene.primitives.remove(item);
          } else {
            // 单数索引处理逻辑
            viewer.entities.removeById(item);
          }
        });
      if (this._changeDepth) viewer.scene.globe.depthTestAgainstTerrain = false;
    }
}